package cn.javabb.web.controller.biz;

import cn.javabb.common.exception.ForbiddenException;
import cn.javabb.common.model.ResponseModel;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.query.CommentQuery;
import cn.javabb.core.service.biz.CommentService;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/12/25 14:50
 */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController {

    @Autowired
    private CommentService commentService;

    /*用户评论列表*/
    @RequiresAuthentication
    @ResponseBody
    @GetMapping("/blogComments")
    public Object blogComments(CommentQuery query){
        query.setUserId(getLoginUser().getId());
        Page p = commentService.listBlogComments(query);
        return ResponseModel.table(p);
    }
    /*用户评论列表*/
    @RequiresAuthentication
    @ResponseBody
    @GetMapping("/postComments")
    public Object postComments(CommentQuery query){
        query.setUserId(getLoginUser().getId());
        Page p = commentService.listPostComments(query);
        return ResponseModel.table(p);
    }
    /*删除评论*/
    @RequiresAuthentication
    @ResponseBody
    @PostMapping("/del")
    public Object del(Integer commentId) {
        if (!commentService.judgeSameUser(commentId)) {
            throw new ForbiddenException();
        }
        if (commentService.deleteById(commentId)>0) {
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Success("删除失败");
    }
}
